package io.renren.util;

import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class JsonUtils {

    public static Gson gson = new Gson();

    public static Map<String, Integer> mapFromJson(String json) {

        return gson.fromJson(json, new TypeToken<Map<String, Integer>>() {
        }.getType());
    }

    public static <T> T json2Obj(String json, Class<T> cls) {

        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(json, cls);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }

        return t;
    }

    public static String map2json(Map<String, Object> map) {
        if (map.size() == 0) {
            return null;
        }
        return gson.toJson(map);

    }

    public static String obj2Json(Object obj) {
        String json = gson.toJson(obj);
        return json;
    }

}
